package com.iamk.feature;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.iamk.segment.SRMSegment;
import com.iamk.util.Convert;
import com.iamk.util.GetData;

public class FColorMSCR {
	int height;
	int width;
	BufferedImage img;
	public int[][] mask;
	String fileMask;
	Map m=new HashMap<String,List>();
	SRMSegment srm;


	public FColorMSCR(BufferedImage img, String fileMask, int minPixel) {
		// TODO Auto-generated constructor stub
		this.img = img;
		this.fileMask = fileMask;
	}
	public FColorMSCR(BufferedImage img, String name, String fileMask) {
		this.img = img;
		this.fileMask = fileMask;
		if(fileMask.equalsIgnoreCase("")){
			srm = new SRMSegment(img, name);
			this.mask = srm.mask;
		}
	}

	private void init() {
		// TODO Auto-generated method stub
		height = img.getHeight();
		width = img.getWidth();
		if (mask == null) {
			mask = GetData.readMaskFile(fileMask);
			
		}
//		mask=T(mask);
		// System.out.println(maxRegion);
	}

	public ArrayList<float[]> extract() {
		ArrayList<float[]> result = new ArrayList<float[]>();
		float[] hsvPixel = new float[3];
		ArrayList<float[]> temp;
		init();
		Color pixelColor = null;
		for (int x = 0; x < height; x++) {
			for (int y = 0; y < width; y++) {
				if (mask[x][y] > 0) {
					pixelColor = new Color(img.getRGB(y, x));
					// hsvPixel = new float[3];
					Color.RGBtoHSB(pixelColor.getRed(), pixelColor.getGreen(),
							pixelColor.getBlue(), hsvPixel);
//					regions[mask[x][y] - 1].pixels.add(hsvPixel.clone());
					temp=(ArrayList<float[]>) m.get(Integer.toString(mask[x][y]));
					if(temp==null){
						temp=new ArrayList<>();
						temp.add(hsvPixel.clone());
						m.put(Integer.toString(mask[x][y]), temp);
					}else {
						temp.add(hsvPixel.clone());
						m.put(Integer.toString(mask[x][y]), temp);
					}
				}
			}
		}
		String label=null;
		int loop=0;
		Region[] regions=new Region[m.size()];
		for (int i = 0; i < regions.length; i++) {
			regions[i] = new Region();
		}
		Iterator<String> keySetIterator = m.keySet().iterator();
		while (keySetIterator.hasNext()) {
            label = keySetIterator.next();
            temp=(ArrayList<float[]>) m.get(label);
            regions[loop].pixels=(ArrayList<float[]>) temp.clone();
            regions[loop].label=Integer.parseInt(label);
            
            loop++;
		}
		
		for (int i = 0; i < regions.length; i++) {
				regions[i].extract();
				result.add(regions[i].featuresLabel);
		}
//		for(float[] i:result){
//			for(int j=0;j<i.length;j++){
//				System.out.print(i[j]+" ");
//			}
//			System.out.println();
//		}
//		 System.out.println("Size region "+ result.size());
		return result;
	}
	private int[][] T(int[][] mask){
		int[][] maskT=new int[mask[0].length][mask.length];
		for(int i=0;i<mask.length;i++)
			for(int j=0;j<mask[0].length;j++){
			maskT[j][i]=mask[i][j];
		}
		return maskT;
	}
}
